<?php
class WxMsgAction extends UserAction{
	
	public function index(){
	
		$where['token'] = session('token');
		$where['status'] = 0;
		$yestoday = mktime(date("H"),0,0,date("m"),date("d")-1,date("Y"));
		$where['addtime'] = array('gt',$yestoday);
	
		$wxmsg = M('wxmsg');
	
		$msgcount =$wxmsg->where($where)->count();
	
		$this->assign('msgcount', $msgcount);
		$this->display();
	}
	
	/**
	 * 未回答的消息
	 */
	public function unanswer(){
		
		$where['token'] = session('token');
		$where['status'] = 0;
		$yestoday = mktime(date("H"),0,0,date("m"),date("d")-2,date("Y"));
		$where['addtime'] = array('gt',$yestoday);
		
		$wxmsg = M('wxmsg');
		
		$count=$wxmsg->where($where)->count();
		$page=new Page($count,10);
		$wxmsgs=$wxmsg->where($where)->limit($page->firstRow.','.$page->listRows)->select();
		
		$this->assign('wxmsgs',$wxmsgs);
		$this->assign('page',$page->show());
		$this->display();
	}
	
	/**
	 * 人工回复消息页面
	 */
	public function answer(){
		$id = $this->_get('id');
		
		if(empty($id)){
			$this->error('错误的访问');
			return;
		}
		
		$msg = M('wxmsg')->find($id);
		
		$this->assign('msg',$msg);
		$this->display();
	}
	
	/**
	 * 所有消息页面
	 */
	public function allmsg(){
		$where['token'] = session('token');
		$where['msgtype'] = 'text';
		$wxmsg = M('wxmsg');
		
		/* $mtype = $_POST['msgtype_select'];
		$minput = $_POST['inputkeyword'];
		
		if(isset($mtype)&& $mtype != '-1'){
			$where['msgtype'] = $mtype;
		}
		if(!empty($minput)){
			$where['msgcontent'] = array('like',"%$minput%");
		} */
		
		$count=$wxmsg->where($where)->count();
		$page=new Page($count,10);
		$wxmsgs=$wxmsg->order('id desc')->where($where)->limit($page->firstRow.','.$page->listRows)->select();
		
		$openids = array();
		foreach ($wxmsgs as $wm){
			array_push($openids, $wm['openid']);
		}
		$openids = array_unique($openids);
		$swhere['openid'] = array('in',implode(',', $openids));
		$subs = M('subscriber')->where($swhere)->getField('openid,nickname');
		//foreach ()
		
		
		$this->assign('subs',$subs);
		$this->assign('wxmsgs',$wxmsgs);
		$this->assign('page',$page->show());
		
		$this->display();
	}
	
	/**
	 * 向微信的服务器发送消息
	 */
	public function answerwx() {
		$where ['id'] = $_POST ['id'];
		$response = $_POST ['response'];
		
		if (strlen ( $response ) == 0) {
			$this->error ( '回复内容不能为空', U ( MODULE_NAME . '/allmsg' ) );
		} else {
			$wxmsg = M ( 'wxmsg' );
			$msg = $wxmsg->find ( $id );
			
			$result = WechatAdvanceAPI::PostMsg ( array (
					'content' => urlencode ( $response ) 
			), $msg ['openid'], 'text' );
			
			if ($result === false) {
				$this->error ( '操作失败，请检查你的AppId和AppSecret，并确保回复的请求是在48小时内的', U ( MODULE_NAME . '/allmsg' ) );
			} else {
				$data ['status'] = 2;
				$data ['response'] = $response;
				$data ['manualtime'] = time ();
				$data ['manualname'] = $this->wecha ['wxname'];
				
				$wxmsg->where ( $where )->save ( $data );
				
				$this->success ( '操作成功', U ( MODULE_NAME . '/allmsg' ) );
			}
		}
	}
	
}